<?php
function seitcm_get_current_date($day=0,$month=0,$year=0){
	return date('d/m/Y',mktime(date("G")+7, date("i"), date("s"), date("m")+$month, date("d")+$day, date("Y")+$year));
}
function seitcm_get_current_datetime($h=0,$ph=0,$s=0,$day=0,$month=0,$year=0){
	return date('d/m/Y G:i:s',mktime(date("G")+7+$h, date("i")+$ph, date("s")+$s, date("m")+$month, date("d")+$day, date("Y")+$year));
}
function seitcm_get_current_sql_datetime($h=0,$ph=0,$s=0,$day=0,$month=0,$year=0){
	return date('Y-m-d G:i:s',mktime(date("G")+7+$h, date("i")+$ph, date("s")+$s, date("m")+$month, date("d")+$day, date("Y")+$year));
}
function seitcm_get_current_sql_date($h=0,$ph=0,$s=0){
	return date('Y-m-d G:i:s',mktime($h, $ph, $s, date("m"), date("d"), date("Y")));
}
function seitcm_get_month_from_mysqldatetime($seitcm_datetime_input){
	$date = seitcm_create_date_from_mysql($seitcm_datetime_input);
	$parts = explode('/', $date);
	return $parts[1];
}
function seitcm_get_month_form_date($date){
	$parts = explode('/', $date);
	return $parts[1];
}
function seitcm_plus_date($date,$d=0,$m=0,$y=0){
	$parts = explode('/', $date);
	return date('d/m/Y',mktime(0, 0, 0, $parts[1]+$m, $parts[0]+$d, $parts[2]+$y));
}
/**
 * 
 * @param unknown $date1
 * @param unknown $date2
 * @return boolean
 */
function seitcm_compare_date($date1,$date2){
	return strtotime(seitcm_create_date_for_mysql2($date1)) > strtotime(seitcm_create_date_for_mysql2($date2));
}
/**
 * Conver d/m/Y G:i date to Y-m-d G:i:s
 * 
 * @param string $seitcm_date_input
 * @return string
 */
function seitcm_create_datetime_for_mysql($seitcm_datetime_input)
{
	$partsdatetime = explode(' ', $seitcm_datetime_input);
	$date = $partsdatetime[0];
	$parts = explode('/', $date);
	return $parts[2].'-'.$parts[1].'-'.$parts[0] . ' ' . $partsdatetime[1] . ':00';
}
	
/**
 * Conver d/m/Y date to Y-m-d 
 * 
 * @param string $seitcm_date_input
 * @return string
 */

function seitcm_create_date_for_mysql($seitcm_date_input,$endday = false)
{
	$parts = explode('/', $seitcm_date_input);
	return $endday ? $parts[2].'-'.$parts[1].'-'.$parts[0].' 23:59:59' : $parts[2].'-'.$parts[1].'-'.$parts[0].' 00:00:00';
}
function seitcm_create_date_for_mysql2($seitcm_date_input)
{
		$parts = explode('/', $seitcm_date_input);
		return $parts[2].'-'.$parts[1].'-'.$parts[0];
}
/**
 * Conver Y-m-d H:i:s time to d/m/Y
 * 
 * @param string $seitcm_datetime_input
 * @return string
 */
function seitcm_create_date_from_mysql($seitcm_datetime_input)
{
	$partsdatetime = explode(' ', $seitcm_datetime_input);
	$mysqldate = $partsdatetime[0];
	$parts = explode('-', $mysqldate);
	return $parts[2].'/'.$parts[1].'/'.$parts[0];
}
/**
 * Conver Y-m-d H:i:s time to d/m/Y H:i:s
 * 
 * @param unknown $seitcm_datetime_input
 * @return string
 */
function seitcm_create_datetime_from_mysql($seitcm_datetime_input)
{
	$partsdatetime = explode(' ', $seitcm_datetime_input);
	$mysqldate = $partsdatetime[0];
	$parts = explode('-', $mysqldate);
	return $parts[2].'/'.$parts[1].'/'.$parts[0].' '.$partsdatetime[1];
}
/**
 * 
 * @param string $str
 * @return mixed
 */
function seitcm_textsignfilter($str){
	$sign=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ","ặ","ẳ","ẵ",
			"è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ",
			"ì","í","ị","ỉ","ĩ",
			"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ","ờ","ớ","ợ","ở","ỡ",
			"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
			"ỳ","ý","ỵ","ỷ","ỹ",
			"đ",
			"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă","Ằ","Ắ","Ặ","Ẳ","Ẵ",
			"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
			"Ì","Í","Ị","Ỉ","Ĩ",
			"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ","Ờ","Ớ","Ợ","Ở","Ỡ",
			"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
			"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
			"Đ");
	$unsign=array("a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a",
			"e","e","e","e","e","e","e","e","e","e","e",
			"i","i","i","i","i",
			"o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",
			"u","u","u","u","u","u","u","u","u","u","u",
			"y","y","y","y","y",
			"d",
			"A","A","A","A","A","A","A","A","A","A","A","A"
			,"A","A","A","A","A",
			"E","E","E","E","E","E","E","E","E","E","E",
			"I","I","I","I","I",
			"O","O","O","O","O","O","O","O","O","O","O","O"
			,"O","O","O","O","O",
			"U","U","U","U","U","U","U","U","U","U","U",
			"Y","Y","Y","Y","Y",
			"D");
	return str_replace($sign,$unsign,$str);
}
/**
 * 
 * @param string $str
 * @return mixed
 */
function seitcm_specialcharfilter($str){
	$special=array("`","~","!","@","#","$","%","^","&","*","(",")","-","_","=",
			"+","/","?",".",",",">","<","\\","|","'","\"",";",":","]","[","}","{");
	$nullspecial=array("","","","","","","","","","","","","","","",
			"","","","","","","","","","","","","","","","","");
	return str_replace($special,$nullspecial,$str);
}
/**
 *  Check date valind with format 'd/m/Y'
 * @param string $datestr
 * @return boolean
 */
function seitcm_check_date_valid($datestr)
{
	if (DateTime::createFromFormat('d/m/Y', $datestr) !== FALSE) {
		return true;
	}
	else return false;
}
/**
 *  Check datetime valind with format 'd/m/Y G:i:s'
 * @param string $datestr
 * @return boolean
 */
function seitcm_check_datetime1_valid($datestr)
{
	if (DateTime::createFromFormat('d/m/Y G:i:s', $datestr) !== FALSE) {
		return true;
	}
	else return false;
}
/**
 * Check datetime valind with format 'd/m/Y G:i'
 * @param string $datestr
 * @return boolean
 */
function seitcm_check_datetime2_valid($datestr)
{
	if (DateTime::createFromFormat('d/m/Y G:i', $datestr) !== FALSE) {
		return true;
	}
	else return false;
}
/**
 *  Check datetime valind with custom format 
 *  Ex: d-m-Y G:i:s, d-m-Y G:i, d-m-Y ...
 * @param unknown $datestr
 * @param unknown $datetimeformat
 * @return boolean
 */
function seitcm_check_datetime_valid($datestr,$datetimeformat)
{
	if (DateTime::createFromFormat($datetimeformat, $datestr) !== FALSE) {
		return true;
	}
	else return false;
}
/**
 * 
 * @param unknown $str
 * @return string
 */
function seitcm_strip_tag($str)
{
	return strip_tags($str,'<strong><em>');
}
/**
 * 
 * @param unknown $text
 * @param unknown $length
 * @return Ambigous <mixed, string>
 */
function seitcm_cut_out_string($text, $length) {
	$text = seitcm_strip_tag($text);
	$length = abs((int)$length);
	if(strlen($text) > $length) {
		$text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1...', $text);
	}
	return($text);
}
/**
 * 
 * @param unknown $key
 * @return string
 */
function seitcm_get_learn_time_string($key)
{
	global $seitcm_learntime;
	foreach ($seitcm_learntime as $seitcm_learntimes)
	{
		if($seitcm_learntimes['status'] && $seitcm_learntimes['key']==$key){
			global $seitcm_date_of_week;
			$parts = explode(',', $seitcm_learntimes['Value']);
			$return = $seitcm_learntimes['duration'] . ' on ';
			foreach ($parts as $part)
			{
				$return .= $seitcm_date_of_week[$part]['sorthand'] . ', ';
			}
			return substr($return,0,-2);
		}
	}
	return '';
}
/**
 * 
 * @param unknown $key
 * @return string
 */
function seitcm_get_learn_time_string_vi($key)
{
	global $seitcm_learntime;
	foreach ($seitcm_learntime as $seitcm_learntimes)
	{
		if($seitcm_learntimes['status'] && $seitcm_learntimes['key']==$key){
			global $seitcm_date_of_week;
			$parts = explode(',', $seitcm_learntimes['Value']);
			$return = $seitcm_learntimes['duration'] . ' các ngày ';
			foreach ($parts as $part)
			{
				$return .= $seitcm_date_of_week[$part]['vn'] . ', ';
			}
			return substr($return,0,-2);
		}
	}
	return '';
}
/**
 * 
 * @param unknown $schoolkey
 * @param unknown $classkey
 * @return string
 */
function seitcm_get_school_string($schoolkey,$classkey)
{
	global $seitcm_schools;
	foreach ($seitcm_schools as $school)
	{
		if($school['status'] && $school['key']==$schoolkey){
			foreach ($school['class'] as $class){
				if($class['key']==$classkey)
					return $class['classname'] . ' - ' . $school['school'];
			}
		}
	}
	return '';
}

function seitcm_get_DOW($datestring)
{
	$parts = explode('/', $datestring);
	$dow = date( "w", mktime(0, 0, 0, $parts[1], $parts[0], $parts[2]));
	return $dow==0 ? $dow : $dow+1;
}
/**
 * 
 * @param unknown $key
 * @return Ambigous <string, string, Ambigous <string, mixed>>|boolean
 */
function seitcm_get_skill_name($key){
	global $seitcm_score_skill;
	foreach ($seitcm_score_skill as $skill)
	{
		if($skill['key'] == $key) return $skill['Value'];
	}
	return false;
}

function seitcm_check_current_user_role($role){
	global $current_user;
	foreach ($current_user->roles as $roles)
	{
		if($roles == $role) return true;
	}
	return false;
}
?>